How to interface RTC module with Arduino and ESP Board

Here in this article, you will learn about the RTC module and how to interface this module with Arduino and ESP boards like Arduino MEGA, Arduino UNO, Arduino NANO, ESP32, Node MCU, and Arduino NANO.

How to interface RTC module with Arduino and ESP Board

RTC acronymic for Real-Time Clock is an electronic Integrated Circuit (IC) that keeps track of time around the clock. It is more like a computer clock where once we set it up gives us accurate time even after the power outage. Before we delve into the interfacing of RTC with ESP32 and another microcontroller, how about we check some facts about RTC? But wait, what is ESP32? ESP32 is a microcontroller chip with Wi-Fi and a dual-mode Bluetooth system integrated into it. It is used in IoT devices, electronics, and of course mobile phones.

how to interface rtc with arduino and esp boardFigure 1: RTC with Different Development Board

How does RTC keep track of time?

RTC module contains a crystal oscillator of 32.768KHz (generally). This is the same frequency used in digital and quartz clocks. In a second, it oscillates about 211 = 32,768 Hz and uses an updated binary counter to track time. Initially, RTC doesn’t know the correct time thus at first, we have to set a current time. This time is saved in RTC’s internal memory which updates over time with no or negligible error.

Does RTC need an uninterrupted power supply?

Every electronic device requires an uninterrupted power supply. RTC, being a computer clock, an electronic device, is not an exception either. So, Yes – to keep tracking the current time and keep updating accordingly, RTC requires an uninterrupted power supply. And hence, it becomes obvious that with a power outage, there is no update of the current time. Thus, it has a battery backup for continuous timekeeping. It requires very low power (continuous less than 1uW) thus interruption of primary power supply doesn’t affect current time because of the in-built lithium battery.

Why do we need RTC?

Almost all microcontrollers have internal timers; they do have oscillators but we still need RTC for time-keeping functions. Like, we would want to keep track of every second, every minute, hour or day, month, year so on and so forth, wouldn’t we? Let’s validate what we just came to know. Let’s look at an example below.

AVR microcontrollers (Arduino) have a built-in timekeeper function called millis() function. By using this function one can track second, minute, hour, day, or even a month. This millis() function needs a continuous power supply to keep tracking time and it sets to zero when the power supply is interrupted i.e. whenever an Arduino or a microcontroller resets (either due to failure of power supply or using reset switch) millis() function starts to count from zero millisecond.

Even if we maintain a continuous power supply to the microcontroller, its timekeeper resets after a fixed time interval. The millis() function is of an unsigned long type variable. The unsigned long variable is of 4 bytes or we can say 32-bits. The maximum value that millis can hold is

232 = 4294967296 milliseconds

i.e. 49 days and 17 hours.

When the millis function increases by a millisecond from 4,294,967,296, it rolls over to zero!

These are the reasons for using RTC instead of microcontroller internal timekeeping.

How to calculate the accuracy of RTC?

Generally, the accuracy of crystal RTC is about \pm 100 to \pm 20 per million. Let’s convert this to second:

\pm \dfrac{100}{1000000} = \pm \dfrac{1}{10000}

i.e. there is the deviation of 1 second in every 10000 seconds which is equal to 8.6 seconds per day.

Similarly,

\pm \dfrac{20}{1000000} = \pm \dfrac{1}{50000}

i.e. there is the deviation of 1 second in every 50000 seconds which is equal to 1.7 seconds per day.

So, the accuracy of general-purpose crystal RTC is between 1.7seconds to 8.6 seconds.

The value of accuracy differs or we can also say that it is low for temperature-compensated crystal oscillator RTC (DSB3231).

crystal aging

Figure 2: DS3231 Datasheet showing Crystal Aging

According to its datasheet for the first year, its accuracy is about \pm 1 ppm i.e. there’s a deviation of 1 second in every 1 million seconds. If we calculate this time in the day, then for the first year there might be a deviation of 1 second in every 11.57 days. This is quite good and the error in time is negligible but this accuracy is not the same for a lifetime due to the crystal aging effect. After the 1st year, its accuracy decreases to \pm 5 ppm i.e. 1 second in every 200000 seconds i.e. there would be a deviation of 1 second in every 2.32 days. Even after a year there would be a minor deviation of about 2.62 minutes and is considered negligible.

To overcome this error, one can synchronize RTC with NTP (Network Time Protocol).

Check out the article Arduino NTP Clock using Arduino: Arduino NTP Clock using NodeMCU and DS3231

Circuit Description How to interface RTC module with Arduino and ESP Board

Figure 1 shows the circuit, built around ESP32, RTC Module DS3231, and I2C LCD. The connection of this component is very simple and straightforward. RTC module and LCD share common I2C pins (GPIO 21 and GPIO 22).

circuit diagram of How to interface RTC module with Arduino and ESP Board

Figure 3: Circuit Diagram of How to interface RTC  with Arduino and ESP Board

Power Supply: Vin pin of ESP32, Vcc pin of LCD and DS3231 is sorted together and connected to +5V supply and ground of all these components is connected to ground pin of LCD.

Note: LCD requires +5V to operate where DS3231 can work with 3.3V also.

author prototype of how to interface rtc with esp32Figure 4: Author Prototype of How to Interface RTC with ESP32

Software Code:

Software code is written in Arduino programming language (based on C/C++), before uploading code change the current date, remove the comment and upload it. After uploading, add the comment to this time setup line and re-upload it.

Software Code:

Leave a Comment